package Homework.cms.articles;

import Homework.cms.util.Page;
import com.xcms.test.news.News;
import com.xcms.test.news.NewsDao;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

public class ArticlesServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String action=request.getParameter("action");
        //为了显示用户作品的网址
        if("page".equals(action)){
            int currPage=Integer.parseInt(request.getParameter("id"));
            Page<Articles> page=ArticlesDao.getPage(1,currPage);
            request.setAttribute("page",page);
            request.getRequestDispatcher("articles.jsp").forward(request,response);
        }
        //在前台显示所有用户的作品（倒叙）
        if("userpage".equals(action)){
            int currPage=Integer.parseInt(request.getParameter("currPage"));
            Page<Articles> page=ArticlesDao.allusersarticles(5,currPage);
            request.setAttribute("page",page);
            request.getRequestDispatcher("allarticles.jsp").forward(request,response);
        }
        //在前台显示本用户所有作品（倒叙）
        if("mypage".equals(action)){
            String writter=request.getParameter("writter");
            Articles articles=new Articles();
            articles.setWritter(writter);
            int currPage=Integer.parseInt(request.getParameter("currPage"));
            Page<Articles> page= ArticlesDao.usersarticles(5,currPage,articles);
            request.setAttribute("userpage",page);
            request.getRequestDispatcher("myarticles.jsp").forward(request,response);
        }
    }
}
